package com.xxx.services;

import com.xxx.mapper.EmployeeMapper;
import com.xxx.pojo.Employee;
import com.xxx.util.GetIdTime;
import com.xxx.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.Scanner;

public class MerchantRegister {
    Scanner sc =new Scanner(System.in);
    String name = null; //姓名
    String username = null; //用户名
    String password = null; // 密码
    String phone = null; //手机号
    String sex = null; // 性别
    String idNumber = null; //身份证号码


    private static SqlSessionFactory factory = SqlSessionFactoryUtil.getSqlSessionFactory();

    /**
     * 注册操作
     */
    public void merchantRegister(){
        //获取对应的方法
        SqlSession sqlSession = factory.openSession();
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);


        Long id = new Long(GetIdTime.getId()+ "1");
        System.out.println("=======商家注册======");
        System.out.println("请输入姓名：");
        while (true) {
            name = sc.next();
            if (name.length()>7)
                System.out.println("姓名过长，请重新输入：");
            else
                break;
        }
        System.out.println("请输入用户名：");

        while (true) {
            username = new Scanner(System.in).next();
            if (username.length() > 32)
                System.out.println("用户名过长，请重新输入：");
            else {
                Employee employee = mapper.selectByUsername(username);
                if (employee == null)
                    sqlSession.close();
                    break;
            }
        }


        System.out.println("请输入密码：");
        while (true) {
            password = sc.next();
            if(password.length()>32)
                System.out.println("密码过长，请重新输入：");
            else
                break;
        }
        System.out.println("请确认密码：");
        while (true) {
            String password2 = sc.next();
            if(!password2.equals(password))
                System.out.println("两次密码不一致，请重新输入：");
            else
                break;
        }
        System.out.println("请输入手机号：");
        while (true) {
            Scanner s = new Scanner(System.in);
            try {
                phone = "" + s.nextLong();
                if(phone.length()!=11)
                    System.out.println("手机号错误，请重新输入：");
                else
                    break;
            } catch (Exception e) {
                phone = null;
                System.out.println("手机号错误，请重新输入：");
            }

        }
        System.out.println("请输入性别：");
        while (true) {
            sex = sc.next();
            if(sex.equals("女") && sex.equals("男"))
                System.out.println("性别输入错误，请重新输入：");
            else
                break;
        }
        System.out.println("请输入身份证号码：");
        while (true) {
            Scanner s = new Scanner(System.in);
            idNumber = null;
            try {
                idNumber = "" + s.nextLong();

                if(idNumber.length() != 18)
                    System.out.println("身份证号输入错误，请重新输入：");
                else
                    break;
            } catch (Exception e) {
                System.out.println("身份证号码输入错误，请重新输入：");

            }

        }
        String createDataTime = GetIdTime.getDataTime();
        String upDateTime = createDataTime;
        Employee employee = new Employee();
        employee.setId(id);
        employee.setName(name);
        employee.setUsername(username);
        employee.setPassword(password);
        employee.setPhone(phone);
        employee.setSex(sex);
        employee.setIdNumber(idNumber);
        employee.setStatus(1);
        employee.setCreateTime(createDataTime);
        employee.setUpdateTime(upDateTime);
        employee.setCreateUser(id);
        employee.setUpdateUser(id);
        sqlSession = factory.openSession();
        mapper = sqlSession.getMapper(EmployeeMapper.class);
        int i = mapper.addEmployee(employee);
        if (i != 0) {
            System.out.println("注册成功");
            sqlSession.commit();
            sqlSession.close();
        } else
            System.out.println("注册失败");
    }

//        try {
//            String sql = "INSERT INTO employee VALUES(?,?,?,?,?,?,?,1,?,?,?,?)";
//            conn = DBUtil.getConnection();
//            pstmt = conn.prepareStatement(sql);
//            pstmt.setLong(1,id);
//            pstmt.setString(2,name);
//            pstmt.setString(3,username);
//            pstmt.setString(4,password);
//            pstmt.setString(5,phone);
//            pstmt.setString(6,sex);
//            pstmt.setString(7,idNumber);
//            pstmt.setString(8,createDataTime);
//            pstmt.setString(9,upDateTime);
//            pstmt.setLong(10,id);
//            pstmt.setLong(11,id);
//
//
//            int i = pstmt.executeUpdate();
//            if (i != 0){
//                System.out.println("注册成功");
//            }
//            else
//                System.out.println("注册失败");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        DBUtil.close(conn,pstmt);




}
